<!doctype html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>反转链表 - 算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-header {
            background: #2d2d2d;
            padding: 12px 20px;
            border-bottom: 1px solid #3e3e3e;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .code-content {
            padding: 20px;
            overflow-x: auto;
        }
        .code-content pre {
            margin: 0;
            font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        .keyword { color: #569cd6; }
        .type { color: #4ec9b0; }
        .comment { color: #6a9955; }
        .string { color: #ce9178; }
        .function { color: #dcdcaa; }
        .number { color: #b5cea8; }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            margin-top: -0.1rem;
            color: #667eea;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .complexity-badge {
            display: inline-flex;
            align-items: center;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 0.875rem;
            font-weight: 500;
            margin-right: 8px;
        }
        .time-complexity {
            background: rgba(102, 126, 234, 0.1);
            color: #667eea;
        }
        .space-complexity {
            background: rgba(118, 75, 162, 0.1);
            color: #764ba2;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-4xl mx-auto text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">反转链表</h1>
            <p class="text-xl md:text-2xl opacity-90 font-light">掌握链表操作的基础算法</p>
            <div class="mt-8 flex justify-center space-x-4">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-clock mr-2"></i>时间复杂度 O(n)
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-layer-group mr-2"></i>空间复杂度 O(1)
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Problem Description -->
        <section class="mb-12">
            <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                <h2 class="text-3xl font-bold mb-6 flex items-center">
                    <i class="fas fa-question-circle text-purple-600 mr-3"></i>
                    题目描述
                </h2>
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="drop-cap serif-font">反</span>转一个单链表。例如，输入链表 1→2→3→4→5，反转后变为 5→4→3→2→1。要求：一次遍历完成（即时间复杂度为 O(n)）。
                </p>
                <div class="mt-6 p-4 bg-purple-50 rounded-lg border-l-4 border-purple-500">
                    <p class="text-sm text-purple-800">
                        <i class="fas fa-lightbulb mr-2"></i>
                        <strong>核心考点：</strong>指针操作、递归思维
                    </p>
                </div>
            </div>
        </section>

        <!-- Visual Representation -->
        <section class="mb-12">
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <h2 class="text-3xl font-bold mb-6 flex items-center">
                    <i class="fas fa-project-diagram text-purple-600 mr-3"></i>
                    算法可视化
                </h2>
                <div class="mermaid">
                    graph LR
                        subgraph "原始链表"
                            A1[1] --> B1[2]
                            B1 --> C1[3]
                            C1 --> D1[4]
                            D1 --> E1[5]
                            E1 --> F1[null]
                        end
                        
                        subgraph "反转过程"
                            A2[1] -.-> B2[2]
                            B2 -.-> C2[3]
                            C2 -.-> D2[4]
                            D2 -.-> E2[5]
                            
                            E2 --> D2
                            D2 --> C2
                            C2 --> B2
                            B2 --> A2
                            A2 --> F2[null]
                        end
                        
                        subgraph "反转后链表"
                            E3[5] --> D3[4]
                            D3 --> C3[3]
                            C3 --> B3[2]
                            B3 --> A3[1]
                            A3 --> F3[null]
                        end
                        
                        style A1 fill:#667eea,stroke:#333,stroke-width:2px,color:#fff
                        style E3 fill:#764ba2,stroke:#333,stroke-width:2px,color:#fff
                </div>
            </div>
        </section>

        <!-- Solution Approaches -->
        <section class="mb-12">
            <h2 class="text-3xl font-bold mb-8 text-center gradient-text">解题思路</h2>
            <div class="grid md:grid-cols-2 gap-6">
                <!-- Iterative Approach -->
                <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-sync-alt text-purple-600 text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold">迭代法</h3>
                    </div>
                    <p class="text-gray-700 mb-4">
                        使用三个指针（<code class="bg-gray-100 px-2 py-1 rounded text-sm">prev</code>, 
                        <code class="bg-gray-100 px-2 py-1 rounded text-sm">curr</code>, 
                        <code class="bg-gray-100 px-2 py-1 rounded text-sm">next</code>），逐个反转节点的指向。
                    </p>
                    <div class="mt-4">
                        <span class="complexity-badge time-complexity">
                            <i class="fas fa-clock mr-1"></i> O(n)
                        </span>
                        <span class="complexity-badge space-complexity">
                            <i class="fas fa-memory mr-1"></i> O(1)
                        </span>
                    </div>
                    <div class="mt-6 p-4 bg-gray-50 rounded-lg">
                        <p class="text-sm text-gray-600">
                            <i class="fas fa-check-circle text-green-500 mr-2"></i>
                            空间效率最优，适合生产环境
                        </p>
                    </div>
                </div>

                <!-- Recursive Approach -->
                <div class="bg-white rounded-2xl shadow-lg p-8 card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-12 h-12 bg-purple-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-code-branch text-purple-600 text-xl"></i>
                        </div>
                        <h3 class="text-2xl font-bold">递归法</h3>
                    </div>
                    <p class="text-gray-700 mb-4">
                        递归到链表尾部，反转后返回新头节点，逐层调整指针。思路清晰，代码简洁。
                    </p>
                    <div class="mt-4">
                        <span class="complexity-badge time-complexity">
                            <i class="fas fa-clock mr-1"></i> O(n)
                        </span>
                        <span class="complexity-badge space-complexity">
                            <i class="fas fa-memory mr-1"></i> O(n)
                        </span>
                    </div>
                    <div class="mt-6 p-4 bg-gray-50 rounded-lg">
                        <p class="text-sm text-gray-600">
                            <i class="fas fa-info-circle text-blue-500 mr-2"></i>
                            递归栈占用空间，适合理解算法
                        </p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Code Implementation -->
        <section class="mb-12">
            <h2 class="text-3xl font-bold mb-8 text-center gradient-text">代码实现</h2>
            
            <!-- Node Definition -->
            <div class="mb-8">
                <div class="code-block">
                    <div class="code-header">
                        <div class="flex items-center">
                            <i class="fab fa-java text-orange-500 mr-2"></i>
                            <span class="text-gray-300">ListNode.java</span>
                        </div>
                        <span class="text-xs text-gray-500">链表节点定义</span>
                    </div>
                    <div class="code-content">
                        <pre><span class="comment">/**
 * 链表节点定义
 */</span>
<span class="keyword">public class</span> <span class="type">ListNode</span> {
    <span class="keyword">int</span> val;
    <span class="type">ListNode</span> next;
    
    <span class="comment">// 构造函数</span>
    <span class="function">ListNode</span>() {}
    <span class="function">ListNode</span>(<span class="keyword">int</span> val) { <span class="keyword">this</span>.val = val; }
    <span class="function">ListNode</span>(<span class="keyword">int</span> val, <span class="type">ListNode</span> next) { 
        <span class="keyword">this</span>.val = val; 
        <span class="keyword">this</span>.next = next; 
    }
}</pre>
                    </div>
                </div>
            </div>

            <!-- Iterative Solution -->
            <div class="mb-8">
                <div class="code-block">
                    <div class="code-header">
                        <div class="flex items-center">
                            <i class="fas fa-sync-alt text-purple-500 mr-2"></i>
                            <span class="text-gray-300">迭代法实现</span>
                        </div>
                        <span class="text-xs text-gray-500">空间复杂度 O(1)</span>
                    </div>
                    <div class="code-content">
                        <pre><span class="comment">/**
 * 反转链表的迭代实现
 * @param head 链表头节点
 * @return 反转后的链表头节点
 */</span>
<span class="keyword">public</span> <span class="type">ListNode</span> <span class="function">reverseList</span>(<span class="type">ListNode</span> head) {
    <span class="comment">// 前一个节点指针，初始为null</span>
    <span class="type">ListNode</span> prev = <span class="keyword">null</span>;
    <span class="comment">// 当前节点指针，初始为头节点</span>
    <span class="type">ListNode</span> curr = head;
    
    <span class="comment">// 遍历链表</span>
    <span class="keyword">while</span> (curr != <span class="keyword">null</span>) {
        <span class="comment">// 保存下一个节点，因为我们要改变当前节点的next指针</span>
        <span class="type">ListNode</span> nextTemp = curr.next;
        
        <span class